package com.oauth.google.server.controller;

import org.gwtwidgets.server.spring.GWTSpringController;
import org.gwtwidgets.server.spring.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.oauth.common.server.util.OAuthRequestUtil;
import com.oauth.components.client.rpc.exception.SessionExpiredException;
import com.oauth.google.client.beans.orkut.GoogleOrkutModel;
import com.oauth.google.client.beans.orkut.HttpQueryString;
import com.oauth.google.client.beans.orkut.OrkutBirthdayDetails;
import com.oauth.google.client.beans.orkut.OrkutFriendDetails;
import com.oauth.google.client.beans.orkut.OrkutScrapDetails;
import com.oauth.google.client.beans.orkut.OrkutUser;
import com.oauth.google.client.exception.GoogleOAuthException;
import com.oauth.google.client.rpc.orkut.OrkutRemoteService;
import com.oauth.google.server.service.OrkutOAuthService;
import com.oauth.logging.OAuthLogger;

public class GoogleOrkutController extends GWTSpringController implements OrkutRemoteService {

	private static final long serialVersionUID = -5758464341967618224L;

	@Autowired
	private OrkutOAuthService mOrkutOAuthService;

	private OAuthLogger logger = OAuthLogger.getLogger(this.getClass());

	/**
	 * @param pOrkutOAuthService
	 *            the orkutOAuthService to set
	 */
	public void setOrkutOAuthService(OrkutOAuthService pOrkutOAuthService) {
		mOrkutOAuthService = pOrkutOAuthService;
	}

	public GoogleOrkutModel getOrkutData(HttpQueryString pQueryString) throws GoogleOAuthException {
		System.out.println("Entering Google Orkut HomeController...");

		GoogleOrkutModel model = new GoogleOrkutModel();
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		try {
			OrkutUser user = getLoggedInUserInfo(memberId);
			model.setUser(user);
			return model;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Error in fetching orkut home data", e);
			throw new GoogleOAuthException();
		}
	}

	public OrkutBirthdayDetails getOrkutBirthdays() throws GoogleOAuthException {
		try {
			Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
			OrkutBirthdayDetails birthday = mOrkutOAuthService.getUpcomingBirthdays(memberId);
			logger.debug("Entering birthdays");
			return birthday;
		} catch (Exception e) {
			logger.error("Error in fetching birthdays", e);
			throw new GoogleOAuthException();
		}
	}

	public OrkutScrapDetails getOrkutScraps() throws GoogleOAuthException {
		try {
			Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
			OrkutScrapDetails scraps = mOrkutOAuthService.getScraps(memberId);
			return scraps;
		} catch (Exception e) {
			logger.error("Error in fetching scraps", e);
			throw new GoogleOAuthException();
		}
	}

	public OrkutFriendDetails getOrkutFriends() throws GoogleOAuthException {
		try {
			Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
			OrkutFriendDetails friends = mOrkutOAuthService.getFriends(memberId);
			return friends;
		} catch (Exception e) {
			logger.error("Error in fetching orkut friends", e);
			throw new GoogleOAuthException();
		}

	}

	public OrkutUser getLoggedInUserInfo(Long memberId) throws GoogleOAuthException {
		try {
			OrkutUser profile = mOrkutOAuthService.getLoggedInUserInfo(memberId);
			logger.info(profile.toString());
			return profile;
		} catch (Exception e) {
			logger.error("Error in fetching orkut user info", e);
			throw new GoogleOAuthException();
		}
	}

	@Override
	public void updateStatus(String pStatusText) throws SessionExpiredException,
			GoogleOAuthException {
		Long memberId = OAuthRequestUtil.getMemberId(ServletUtils.getRequest());
		try {
			mOrkutOAuthService.updateStatus(pStatusText, memberId);
		} catch (Exception e) {
			logger.error("Error in updating orkut user status for member=" + memberId, e);
			throw new GoogleOAuthException();
		}
	}

}
